# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_VertexArray < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_default_variables
    vary_char = M3G::VertexArray.new 2, 3, 1
    values = vary_char.values[0,2]
    6.times { |i|
      assert_equal 0, values[i]
    }

    vary_short = M3G::VertexArray.new 2, 3, 2
    values = vary_short.values[0,2]
    6.times { |i|
      assert_equal 0, values[i]
    }

    vary_float = M3G::VertexArray.new 2, 3, 4
    values = vary_short.values[0,2]
    6.times { |i|
      assert_equal 0, values[i]
    }

  end

  def test_set_variables_1byte
    vary = M3G::VertexArray.new 100, 3, 1

    vary.values[2,2] = [1,2,3, 4,5,6]   # 2から2個
    
    values = vary.values[0,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]

    values = vary.values[2,2]
    assert_equal 6, values.length
    assert_equal 1, values[0]
    assert_equal 2, values[1]
    assert_equal 3, values[2]
    assert_equal 4, values[3]
    assert_equal 5, values[4]
    assert_equal 6, values[5]

    values = vary.values[4,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]
  end

  def test_set_variables_2byte
    vary = M3G::VertexArray.new 100, 3, 2

    vary.values[2,2] = [1,2,3, 4,5,6]   # 2から2個
    
    values = vary.values[0,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]

    values = vary.values[2,2]
    assert_equal 6, values.length
    assert_equal 1, values[0]
    assert_equal 2, values[1]
    assert_equal 3, values[2]
    assert_equal 4, values[3]
    assert_equal 5, values[4]
    assert_equal 6, values[5]

    values = vary.values[4,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]
  end

  def test_set_variables_4byte
    vary = M3G::VertexArray.new 100, 3, 4

    vary.values[2,2] = [1.5,2.5,3.5, 4.5,5.5,6.5]   # 2から2個
    
    values = vary.values[0,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]

    values = vary.values[2,2]
    assert_equal 6  , values.length
    assert_equal 1.5, values[0]
    assert_equal 2.5, values[1]
    assert_equal 3.5, values[2]
    assert_equal 4.5, values[3]
    assert_equal 5.5, values[4]
    assert_equal 6.5, values[5]

    values = vary.values[4,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]
  end

  def test_duplicate
    vary1 = M3G::VertexArray.new 2, 3, 2
    vary1.values[0,2] = [1,2,3, 4,5,6]
    
    # VertexArrayのduplicateは"深いコピー"
    vary2 = vary1.duplicate

    # vary1の方を書き換えても、
    vary1.values[0,2] = [0,0,0, 0,0,0]
    values = vary1.values[0,2]
    assert_equal 6, values.length
    assert_equal 0, values[0]
    assert_equal 0, values[1]
    assert_equal 0, values[2]
    assert_equal 0, values[3]
    assert_equal 0, values[4]
    assert_equal 0, values[5]

    # vary2の方は変化しない.
    values = vary2.values[0,2]
    assert_equal 6, values.length
    assert_equal 1, values[0]
    assert_equal 2, values[1]
    assert_equal 3, values[2]
    assert_equal 4, values[3]
    assert_equal 5, values[4]
    assert_equal 6, values[5]
  end

  def test_convert
    # charで作ったが、
    vary = M3G::VertexArray.new 2, 3, 1
    vary.values[0,2] = [1,2,3, 4,5,6]

    # 後からfloatに変更した。
    vary.convert 4
    vary.values[1,1] = [4.5,5.5,6.5]
    
    values = vary.values[0,2]
    assert_equal 6  , values.length
    assert_equal 1  , values[0]
    assert_equal 2  , values[1]
    assert_equal 3  , values[2]
    assert_equal 4.5, values[3]
    assert_equal 5.5, values[4]
    assert_equal 6.5, values[5]
  end

end
